<?php

class ProductShopController extends AController {

    public function loadModel($id) {
        $model = DProductShop::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actions() {
        return array(
            'sortOrder' => array(
                'class'        => 'ext.mylib.widgets.OrderColumn.OrderAction',
                'modelClass'   => 'DProductShop',
            ),
            'toggleActive' => array(
                'class'      => 'ext.mylib.widgets.ActiveColumn.ActiveAction',
                'modelClass' => 'DProductShop',
            ),
            'toggleHot'    => array(
                'class'      => 'ext.mylib.widgets.ActiveColumn.ActiveAction',
                'modelClass' => 'DProductShop',
                'column'     => 'hot',
            ),
        );
    }

    public function init() {
        parent::init();
        SpsState::setPrefix('DProductShop');
    }

    public function actionIndex() {
        if (Yii::app()->request->isAjaxRequest) {
            SpsState::writeState('DProductShop');
        }

        $model = new DProductShop('search');
        $model->unsetAttributes();  // clear any default values

        if (isset($_GET['DProductShop']))
            $model->attributes = $_GET['DProductShop'];
        elseif (SpsState::hasState('search'))
            $model->attributes = SpsState::getState('search');

        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {
        $model = new DProductShop;

        if (isset($_POST['DProductShop'])) {
            $model->attributes = $_POST['DProductShop'];

            if ($model->save()) {
                Yii::app()->user->setFlash('message', 'created');
                $model->clearSession();
                $this->redirect(array('index'));
            }
        } else {
            if (Yii::app()->user->hasState('folder_id')) {
                $model->folder_id = Yii::app()->user->getState('folder_id');
            } else {
                $model->folder_id = uniqid();
                Yii::app()->user->setState('folder_id', $model->folder_id);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        Yii::app()->user->setFlash('keepState', true);
        $model = $this->loadModel($id);

        if (isset($_POST['DProductShop'])) {
            $model->attributes = $_POST['DProductShop'];
            if ($model->save()) {
                Yii::app()->user->setFlash('message', 'updated');
                $model->clearSession();
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isAjaxRequest) {
            $this->loadModel($id)->delete();
            $result['success'] = true;
            echo json_encode($result);
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * AJAX SUPPROTING FOR ASSIGNING LINK
     */
    public function actionClearSession($scenario='') {
        $this->detectAjaxRequest();
        $model = new DProductShop();
        $model->clearSession($scenario);
        echo 'success';
    }
    
    private function detectAjaxRequest() {
        if (! Yii::app()->request->isAjaxRequest) {
            throw new CHttpException('403', 'Forbidden');
            Yii::app()->end();
        }
    }

}